perm filename BUGMF.CH[MF,DEK]9 blob sn#782194 filedate 1985-01-23 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00039 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00004 00002	@x Tell WEAVE to print only the changes:
C00005 00003	@x WAITS's banner:
C00006 00004	@x Switches for debugging and statistics:
C00009 00005	@x The INIMF switch:
C00012 00006	@x Compiler directives:
C00014 00007	@x Compile-time constants:
C00022 00008	@x TANGLE-time constants:
C00025 00009	@x System-dependent character set changes:
C00027 00010	@x Opening files:
C00032 00011	@x New input_ln:
C00039 00012	@x Terminal I/O:
C00042 00013	@x Special terminal controls:
C00044 00014	@x Initializing the terminal:
C00050 00015	@x Making special characters printable:
C00051 00016	@x Terminal input:
C00053 00017	@x The `E' option:
C00057 00018	@x Changes for 36-bit machines:
C00059 00019	@x Eliminating addition/subtraction of zero:
C00062 00020	@x Date and time:
C00064 00021	@x Special classes for SAIL character set goodies
C00068 00022	@x Screen routines:
C00076 00023	@x Page number maintenance:
C00078 00024	@x Printing the page number:
C00079 00025	@x More page number maintenance:
C00082 00026	@x Pausing on input:
C00088 00027	@x Parsing file names:
C00099 00028	@x Printing file names:
C00100 00029	@x Converting file names to PASCAL strings:
C00103 00030	@x Parsing file names in the buffer:
C00110 00031	@x The real file names:
C00114 00032	@x Line editor gets misspelled file name:
C00116 00033	@x Reading the first line of a file:
C00118 00034	@x Opening the TFM file:
C00119 00035	@x The GF output buffer:
C00123 00036	@x "r GFtoDVI":
C00124 00037	@x start/stop counting
C00126 00038	@x The endgame:
C00131 00039	@x Final system-dependent changes:
C00144 ENDMK
C⊗;
@x Tell WEAVE to print only the changes:
	\def\?##1]{\hbox to 1in{\hfil##1.\ }}
	}
@y
	\def\?##1]{\hbox{Changes to \hbox to 1em{\hfil##1}.\ }}
	}
\let\maybe=\iffalse
@z
@x WAITS's banner:
@d banner=='This is METAFONT, Version 0.75' {printed when \MF\ starts}
@y [Actually the standard banner is used so that TRAP.LOG looks normal]
@d banner=='This is METAFONT, Version 0.75' {printed when \MF\ starts}
@z
@x Switches for debugging and statistics:
@d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering
	usage statistics}
@d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering
	usage statistics}
@f stat==begin
@f tats==end
@y
Moreover, this version of \MF\ has been instrumented so that runtime
frequency counts can be accumulated by a single user of \MF\ over a
period of time. (In particular, it can be used to see what parts of the
program have been exercised by the two runs of the `\.{TRAP}' test.)

The special code that enables counting is delimited by `$|freq|\ldots|qerf|$'.

@d debug== {yes we are debugging}
@d gubed==@t@>
@f debug==begin
@f gubed==end
@#
@d stat== {change this to `$\\{stat}\equiv\.{@@\{}$' when not
	gathering usage statistics}
@d tats==@t@> {change this to `$\\{tats}\equiv\.{@@\}}$' when not
	gathering usage statistics}
@f stat==begin
@f tats==end
@#
@d freq==@{  {change this to `$\\{freq}\equiv\null$' when
	accumulating frequency counts}
@d qerf==@t@>@} {change this to `$\\{qerf}\equiv\null$' when
	accumulating frequency counts}
@f freq==begin
@f qerf==end
@z
@x The INIMF switch:
@d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version}
@d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version}
@y
@d init== {change this to `$\\{init}\equiv\null$' for \.{INIMF}}
@d tini== {change this to `$\\{tini}\equiv\null$' for \.{INIMF}}
@z
@x Compiler directives:
@{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
@y
@{@&$C-,A+,D-,W+@}
	{no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D:5,W+,Z:@=377777777777B@>@}@+ gubed
	{but turn everything on when debugging}
@!freq @{@&$D:2@}@+ qerf {except there isn't room when we need counters}
{the `|W+|' switch catches more syntax errors}
{the `|Z|' switch sets variables to maxint before they receive a value}
{the `\ignorespaces|D:5|' avoids initial stop for the debugger}
{the `\ignorespaces|D:2|' augments debugger to maintain counters}
@z
@x Compile-time constants:
@!mem_max=30000; {greatest index in \MF's internal |mem| array;
	must be strictly less than |max_halfword|;
	must be equal to |mem_top| in \.{INIMF}, otherwise |≥mem_top|}
@!max_internal=100; {maximum number of internal quantities}
@!buf_size=500; {maximum number of characters simultaneously present in
	current lines of open files; must not exceed |max_halfword|}
@!error_line=72; {width of context lines on terminal error messages}
@!half_error_line=42; {width of first lines of contexts in terminal
	error messages; should be between 30 and |error_line-15|}
@!max_print_line=79; {width of longest text lines output; should be at least 60}
@!screen_width=768; {number of pixels in each row of screen display}
@!screen_depth=1024; {number of pixels in each column of screen display}
@!stack_size=30; {maximum number of simultaneous input sources}
@!max_strings=1500; {maximum number of strings; must not exceed |max_halfword|}
@!string_vacancies=8000; {the minimum number of characters that should be
	available for the user's identifier names and strings,
	after \MF's own error messages are stored}
@!pool_size=32000; {maximum number of characters in strings, including all
	error messages and help texts, and the names of all identifiers;
	must exceed |string_vacancies| by the total
	length of \MF's own strings, which is currently about 20000}
@!move_size=5000; {space for storing moves in a single octant}
@!max_wiggle=100; {number of autorounded points per cycle}
@!gf_buf_size=800; {size of the output buffer, must be a multiple of 8}
@!file_name_size=40; {file names shouldn't be longer than this}
@!pool_name='MFbases:MF.POOL                         ';
	{string of length |file_name_size|; tells where the string pool appears}
@.MFbases@>
@!path_size=100; {maximum number of knots between breakpoints of a path}
@y
@!mem_max=3000; {greatest index in \TeX's internal |mem| array;
	must be strictly less than |max_halfword|;
	must be equal to |mem_top| in \.{INIMF}, otherwise |≥mem_top|;
	this is the value appropriate to the \.{TRAP} test file}
@!max_internal=100; {maximum number of internal quantities}
@!buf_size=500; {maximum number of characters simultaneously present in
	current lines of open files; must not exceed |max_halfword|}
@!error_line=64; {width of context lines on terminal error messages}
@!half_error_line=32; {width of first lines of contexts in terminal
	error messages; should be between 30 and |error_line-15|}
@!max_print_line=72; {width of longest text lines output; should be at least 60}
@!screen_width=100; {number of pixels in each row of screen display}
@!screen_depth=200; {number of pixels in each column of screen display}
@!stack_size=30; {maximum number of simultaneous input sources}
@!max_strings=1500; {maximum number of strings; must not exceed |max_halfword|}
@!string_vacancies=8000; {the minimum number of characters that should be
	available for the user's identifier names and strings,
	after \MF's own error messages are stored}
@!pool_size=32000; {maximum number of characters in strings, including all
	error messages and help texts, and the names of all identifiers;
	must exceed |string_vacancies| by the total
	length of \MF's own strings, which is currently about 20000}
@!move_size=5000; {space for storing moves in a single octant}
@!max_wiggle=100; {number of autorounded points per cycle}
@!gf_buf_size=8; {size of the output buffer, must be a multiple of 8}
@!file_name_size=23; {file names shouldn't be longer than this}
@!pool_name='MF.POOL[MF,SYS]        ';
	{string of length |file_name_size|; tells where the string pool appears}
@!path_size=100; {maximum number of knots between breakpoints of a path}
@!count_name='<!MF!>.TXT[MF,SYS]     '; {frequency counts go here}
@z
@x TANGLE-time constants:
@d mem_min=0 {smallest index in the |mem| array, must not be less
	than |min_halfword|}
@d mem_top==30000 {largest index in the |mem| array dumped by \.{INIMF};
	must be substantially larger than |mem_min|
	and not greater than |mem_max|}
@d hash_size=2100 {maximum number of symbolic tokens,
	must be less than |max_halfword-3*param_size|}
@d hash_prime=1777 {a prime number equal to about 85\% of |hash_size|}
@d max_in_open=6 {maximum number of input files and error insertions that
	can be going on simultaneously}
@d param_size=150 {maximum number of simultaneous macro parameters}
@y
@d mem_min=0 {smallest index in the |mem| array, must not be less
	than |min_halfword|}
@d mem_top==3000 {largest index in the |mem| array dumped by \.{INIMF};
	must be substantially larger than |mem_min|
	and not greater than |mem_max|}
@d hash_size=2100 {maximum number of symbolic tokens,
	must be less than |max_halfword-3*param_size|}
@d hash_prime=1777 {a prime number equal to about 85\% of |hash_size|}
@d max_in_open=6 {maximum number of input files and error insertions that
	can be going on simultaneously}
@d param_size=150 {maximum number of simultaneous macro parameters}
@z
@x System-dependent character set changes:
@↑character set dependencies@>
@↑system dependencies@>

@<Set init...@>=
for i←1 to @'37 do xchr[i]←' ';
@y
@↑character set dependencies@>
@↑system dependencies@>

The code shown here is intended to be used on the Stanford {\sc SAIL} system,
and at other installations like CMU and ISI where essentially the same
extended character set is used. The fact that {\mc SAIL} has |'}'| in the
wrong place turns out to cause no difficulty in this case.

@<Set initial values...@>=
for i←1 to @'37 do xchr[i]←chr(i);
xchr[@'30]←chr(@'137);
xchr[@'32]←chr(@'33); {|not_equal| sign}
xchr[@'33]←chr(@'176);
@z
@x make tabs come in as spaces
for i←1 to @'176 do xord[xchr[i]]←i;
@y
for i←1 to @'176 do xord[xchr[i]]←i;
xord[chr(@'11)]←@'40;
@z
@x Opening files:
@d reset_OK(#)==erstat(#)=0
@d rewrite_OK(#)==erstat(#)=0

@p function a_open_in(var @!f:alpha_file):boolean;
	{open a text file for input}
begin reset(f,name_of_file,'/O'); a_open_in←reset_OK(f);
end;
@#
function a_open_out(var @!f:alpha_file):boolean;
	{open a text file for output}
begin rewrite(f,name_of_file,'/O'); a_open_out←rewrite_OK(f);
end;
@#
function b_open_out(var @!f:byte_file):boolean;
	{open a binary file for output}
begin rewrite(f,name_of_file,'/O'); b_open_out←rewrite_OK(f);
end;
@#
function w_open_in(var @!f:word_file):boolean;
	{open a word file for input}
begin reset(f,name_of_file,'/O'); w_open_in←reset_OK(f);
end;
@#
function w_open_out(var @!f:word_file):boolean;
	{open a word file for output}
begin rewrite(f,name_of_file,'/O'); w_open_out←rewrite_OK(f);
end;
@y
@d reset_OK(#)==erstat(#) mod @'20000 = 0
@d rewrite_OK(#)==erstat(#) mod @'20000 = 0

@p function erstat(var @!f:file):integer; extern;@t/2@>
@#
function a_open_in(var @!f:alpha_file):boolean;
	{open a text file for input}
begin reset(f,name_of_file,'/E/O/N:9');
	{the \.{/E} switch distinguishes |form_feed| from |carriage_return|;
	the \.{/O} switch gives error control to us;
	and the \.{/N:9} switch specifies 9 buffers, which
	seems to work satisfactorily at {\mc SAIL}}
a_open_in←reset_OK(f);
end;
@#
function a_open_out(var @!f:alpha_file):boolean;
	{open a text file for output}
begin rewrite(f,name_of_file,'/O/N:2'); a_open_out←rewrite_OK(f);
end; {two buffers seems adequate for text output files}
@#
function b_open_out(var @!f:byte_file):boolean;
	{open a binary file for output}
begin rewrite(f,name_of_file,'/O/N:9'); b_open_out←rewrite_OK(f);
end;    {here we use |ary_out| so the \.{/B} switch isn't appropriate}
@#
function tfm_b_open_out(var @!f:byte_file):boolean;
	{open a packed binary file for output}
begin rewrite(f,name_of_file,'/B:8/O'); tfm_b_open_out←rewrite_OK(f);
end;
@#
function w_open_in(var @!f:word_file):boolean;
	{open a word file for input}
begin reset(f,name_of_file,'/O/N:9'); w_open_in←reset_OK(f);
end;
@#
function w_open_out(var @!f:word_file):boolean;
	{open a word file for output}
begin rewrite(f,name_of_file,'/O/N:9'); w_open_out←rewrite_OK(f);
end;
@z
@x New input_ln:
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables called
|buffer|, |first|, and |last| that will be described in detail later; for
now, it suffices for us to know that |buffer| is an array of |ASCII_code|
values, and that |first| and |last| are indices into this array
representing the beginning and ending of a line of text.

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@y
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables
called |buffer|, |first|, and |last| that will be described in detail
later; for now, it suffices for us to know that |buffer| is an array of
|ASCII_code| values, and that |first| and |last| are indices into this
array representing the beginning and ending of a line of text.

We will read the lines first into an auxiliary buffer, in order to
save the running time of procedure-call overhead. This uses a nice
feature of \ph\ that Knuth chose not to mention in \MF84.
@↑Knuth, Donald Ervin@>

At {\mc SAIL} we want to recognize page marks (indicated by |form_feed|
characters), and keep track of the current page number.

@d form_feed=@'14 {ASCII code used at end of a page}

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@!aux_buf:array[0..70] of text_char; {where the characters go first}
@↑system dependencies@>
@z
@x
@p function input_ln(var @!f:alpha_file;@!bypass_eoln:boolean):boolean;
	{inputs the next line or returns |false|}
var @!last_nonblank:0..buf_size; {|last| with trailing blanks removed}
begin if bypass_eoln then if not eof(f) then get(f);
	{input the first character of the line into |f↑|}
last←first; {cf.\ Matthew 19\thinspace:\thinspace30}
if eof(f) then input_ln←false
else	begin last_nonblank←first;
	while not eoln(f) do
		begin if last≥max_buf_stack then
			begin max_buf_stack←last+1;
			if max_buf_stack=buf_size then
				overflow("buffer size",buf_size);
@:METAFONT capacity exceeded buffer size}{\quad buffer size@>
			end;
		buffer[last]←xord[f↑]; get(f); incr(last);
		if buffer[last-1]≠" " then last_nonblank←last;
		end;
	last←last_nonblank; input_ln←true;
	end;
end;
@y
@p function input_ln(var @!f:alpha_file;@!bypass_eoln:boolean):boolean;
	{inputs the next line or returns |false|}
label 1,done;
var @!n: integer;
@!k,@!m: 0..buf_size; {indices into |buffer|}
begin if bypass_eoln then {input the first character of the line into |f↑|}
	begin if not eof(f) then get(f);
	if not eof(f) then if f↑=chr(@'12) then get(f); {skip past a |line_feed|}
	end;
last←first;
if eof(f) then input_ln←false
else	begin read(f,aux_buf:n);
	if buffer[first]=form_feed then {previous line was end-of-page}
		begin incr(page); line←1; {adjust line and page numbers}
		end;
1:	if last+n>max_buf_stack then
		if last+n≥buf_size then
			begin max_buf_stack←buf_size;
			overflow("buffer size",buf_size);
@:METAFONT capacity exceeded buffer size}{\quad buffer size@>
			end
		else max_buf_stack←last+n;
	if n>0 then
		begin m←last;
		if n=72 then last←m+71@+else last←m+n;
		for k←m to last-1 do buffer[k]←xord[aux_buf[k-m]];
		if n=72 then {there's more on this line}
			begin read(f,aux_buf:n); goto 1;
			end;
		end
	else if f↑=chr(form_feed) then {end of page}
		begin aux_buf[0]←f↑; n←1; goto 1;
		end;
	loop@+	begin if last=first then goto done;
		if buffer[last-1]≠" " then goto done;
		decr(last);
		end;
done:	input_ln←true;
	end;
end;
@↑system dependencies@>
@z
@x Terminal I/O:
is considered an output file the file variable is |term_out|.
@↑system dependencies@>

@<Glob...@>=
@!term_in:alpha_file; {the terminal as an input file}
@!term_out:alpha_file; {the terminal as an output file}

@ Here is how to open the terminal files
in \ph. The `\.{/I}' switch suppresses the first |get|.
@↑system dependencies@>

@d t_open_in==reset(term_in,'TTY:','/O/I') {open the terminal for text input}
@d t_open_out==rewrite(term_out,'TTY:','/O') {open the terminal for text output}
@y
is considered an output file the file variable is |term_out|.
On WAITS, this point is moot, since we use the built-in |TTY| file.
@↑system dependencies@>

@d term_in==TTY {the terminal as an input file}
@d term_out==TTY {the terminal as an output file}

@ Here is how to open the terminal files on WAITS: we don't do anything,
since |TTY| is always open.  Note that |eoln(term_in)| is initially |true|.
@↑system dependencies@>

@d t_open_in==do_nothing {open the terminal for text input}
@d t_open_out==do_nothing {open the terminal for text output}
@z
@x Special terminal controls:
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == do_nothing {cancel the user's cancellation of output}
@y
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d tty_set==@'400121 {special instruction to {\mc WAITS}}
@d tty_escape_code==@'004000000000 {to simulate user typing ESC}
@d tty_break_code==@'004000000400 {to simulate user typing BREAK}
@d cancel_esc_O==begin if inskp0 then end

@<Error handling procedures@>=
function inskp0:boolean; extern;
procedure wake_up_terminal;
var @!val:integer; {a value that we don't have to look at}
@!success:boolean; {ditto}
begin cancel_esc_O; {cancel the user's cancellation of output}
esc_break[1]:=tty_escape_code + ord('N'); {simulate ESC \.N}
call_i(tty_set,-1,esc_break,val,success);
end;
@z
@x Initializing the terminal:
@ The following program does the required initialization
without retrieving a possible command line.
It should be clear how to modify this routine to deal with command lines,
if the system permits them.
@↑system dependencies@>

@p function init_terminal:boolean; {gets the terminal input started}
label exit;
begin t_open_in;
loop@+begin wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
	if not input_ln(term_in,true) then {this shouldn't happen}
		begin write_ln(term_out);
		write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
		init_terminal←false; return;
		end;
	loc←first;
	while (loc<last)∧(buffer[loc]=" ") do incr(loc);
	if loc<last then
		begin init_terminal←true;
		return; {return unless the line was all blank}
		end;
	write_ln(term_out,'Please type the name of your input file.');
	end;
exit:end;
@y
@ The following program does the required initialization
and accepts interrupts and also retrieves a possible command line, using
new system routines due to David R. Fuchs.
@↑Fuchs, David Raymond@>

@d pto_chr(#)==ptwr1w(0,ord(#)) {put a character in the line editor}

@p procedure esci(var @!x:integer); extern; @t\2@>@;
	{increments |x| each time the user types escape-I or break-I;
	the program can change |x| whenever it wants to, but |x| had
	better be a global variable}
@#
function rescan:boolean; extern; @t\2@>@;
	{puts the command line into the terminal buffer,
	or returns |false| if there was no command line}
@#
function tmp_in(f:s@&t@&r@&i@&n@&g;var @!s:s@&t@&r@&i@&n@&g):integer; extern;
	@t\2@>@;
	{reads \.{TMPCOR} file |f| into |s|, and returns its length
		(|≤0| means error)}
@#
function cclsw: boolean; extern; @t\2@>@;
	{was program started with \.{RUN} offset of 1 (i.e., from \.{SNAIL})?}
@#
procedure ptwr1w(pty,c:integer); extern; @t\2@>@;
	{simulates typing of a character on a \.{PTY}}
@#
function init_terminal:boolean; {gets the terminal files started}
label exit;
var @!l:integer; {length returned by |tmp_in|}
@!line_found:boolean; {have we scanned a line?}
@!tmp_cor_buf:packed array[0..100] of char; {where |tmp_in| puts things}
begin t_open_in;
esci(interrupt);
last←first;
if cclsw then {started by \.{MF} monitor command}
	begin l←tmp_in('MF',tmp_cor_buf);
	loc←1;
	while (loc<l)∧(tmp_cor_buf[loc]≠'←') do incr(loc);
	incr(loc);
	while loc<l do
		begin if tmp_cor_buf[loc]>' ' then
			begin buffer[last]←xord[tmp_cor_buf[loc]]; incr(last);
			end;
		incr(loc);
		end;
	end
else
@!debug if false then@;@+gubed@;@/
if rescan then
	begin read_ln(term_in); {get first character into |term_in↑|}
	while (¬ eoln(term_in))∧(term_in↑≠';') do get(term_in);
	if term_in↑=';' then
		begin get(term_in);
		while ¬ eoln(term_in) do
			begin buffer[last]←xord[term_in↑]; incr(last); get(term_in);
			end;
		end;
	end;
line_found←(last>first);
loop@+	begin loc←first;
	while (loc<last)∧(buffer[loc]=" ") do incr(loc);
	if loc<last then
		begin init_terminal←true;
		return; {return unless the line was all blank}
		end;
	if line_found then
		write_ln(term_out,'Please type the name of your input file.');
	wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
	buffer[first]←0; {|input_ln| may look at |buffer[first]|}
	if not input_ln(term_in,true) then {this shouldn't happen}
		begin write_ln(term_out);
		write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
		init_terminal←false; return;
		end;
	line_found←true;
	end;
exit:end;
@↑system dependencies@>
@z
@x Making special characters printable:
@<Character |k| cannot be printed@>=
	(k<" ")∨(k>"~")
@y [no change when running the TRAP test]
@<Character |k| cannot be printed@>=
	(k<" ")∨(k>"~")
@z
@x Terminal input:
@d prompt_input(#)==begin wake_up_terminal; print(#); term_input;
		end {prints a string and gets a line of input}

@p procedure term_input; {gets a line from the terminal}
var @!k:0..buf_size; {index into |buffer|}
begin update_terminal; {Now the user sees the prompt for sure}
@y
@d prompt_input(#)==begin cancel_esc_O; print(#); term_input;
		end {prints a string and gets a line of input}

@p procedure term_input; {gets a line from the terminal}
var @!k:0..buf_size; {index into |buffer|}
begin update_terminal; {Now the user sees the prompt for sure}
buffer[first]←0; {makes sure |input_ln| doesn't find a |form_feed|}
@z
@x The `E' option:
line ready to be edited. But such an extension requires some system
wizardry, so the present implementation simply types out what file should be
edited and the relevant line number.
@y
line ready to be edited. The present implementation does this by loading
the line editor with the appropriate call to the editor. We treat `\.T' the
same as `\.E', because other programs on this system invoke the editor
when the user says `\.T'.
@z
@x
"E": if file_ptr>0 then
	begin print_nl("You want to edit file ");
@.You want to edit file x@>
	print(input_stack[file_ptr].name_field);
	print(" at line "); print_int(line);
	interaction←scroll_mode; jump_out;
	end;
@y
"E","T": if file_ptr>0 then
	begin selector←new_string; pool_ptr←str_start[str_ptr];
	print("et "); print(input_stack[file_ptr].name_field);
	print_char("/"); print_int(page); print("p/");
	print_int(line); print_char("l"); print_char(@'15);
	if str_ptr<max_strings then
		begin pseudo_typein←str_ptr; incr(str_ptr);
		str_start[str_ptr]←pool_ptr;
		end; {|make_string| not declared |forward|}
	selector←term_and_log; interaction←scroll_mode; jump_out;
	end;
@z
@x Changes for 36-bit machines:
The values defined here are recommended for most 32-bit computers.
@y (we make no changes)
The values defined here are recommended for most 32-bit computers.
@z
@x Eliminating addition/subtraction of zero:
@ The operation of subtracting |min_halfword| occurs rather frequently in
\MF, so it is convenient to abbreviate this operation by using the macro
|ho| defined here.  \MF\ will run faster with respect to compilers that
don't optimize the expression `|x-0|', if this macro is simplified in the
obvious way when |min_halfword=0|. Similarly, |qi| and |qo| are used for
input to and output from quarterwords.
@↑system dependencies@>

@d ho(#)==#-min_halfword
	{to take a sixteen-bit item from a halfword}
@d qo(#)==#-min_quarterword {to read eight bits from a quarterword}
@d qi(#)==#+min_quarterword {to store eight bits in a quarterword}
@y We leave in "+0" and "-0" to catch errors of missing parentheses.
@ The operation of subtracting |min_halfword| occurs rather frequently in
\MF, so it is convenient to abbreviate this operation by using the macro
|ho| defined here.  \MF\ will run faster with respect to compilers that
don't optimize the expression `|x-0|', if this macro is simplified in the
obvious way when |min_halfword=0|. So it has been simplified in the obvious way.
Similarly, |qi| and |qo| are used for input to and output from quarterwords.
@↑system dependencies@>

@d qi(#)==0+#+0 {to put an |eight_bits| item into a quarterword}
@d qo(#)==0+#-0 {to take an |eight_bits| item from a quarterword}
@d ho(#)==0+#-0 {to take a sixteen-bit item from a halfword}
@z
@x Date and time:
Since standard \PASCAL\ cannot provide such information, something special
is needed. The program here simply specifies July 4, 1776, at noon; but
users probably want a better approximation to the truth.

Note that the values are |scaled| integers. Hence \MF\ can no longer
be used after the year 32767.

@p procedure fix_date_and_time;
begin internal[time]←12*60*unity; {minutes since midnight}
internal[day]←4*unity; {fourth day of the month}
internal[month]←7*unity; {seventh month of the year}
internal[year]←1776*unity; {Anno Domini}
end;
@y
It uses a {\mc WAITS} monitor call that puts the date in the left 18 bits
and the time in the right 18 bits.

@p procedure fix_date_and_time;
var @!t:integer; {accumulator}
date:integer; {raw date}
g:boolean; {garbage}
begin call_i(@'400101,,t,t,g); {that's \.{ACCTIM}}
date←t div @'1000000;
internal[time]←((t mod @'1000000) div 60)*unity;
internal[day]←((date mod 31)+1)*unity;
internal[month]←(((date div 31) mod 12)+1)*unity;
internal[year]←((date div (31*12))+1964)*unity;
if internal[month]=@'1000000 then if internal[day]=unity then {April Fool}
	wterm_ln('Hello! I am your user-friendly MF System.');
end;
@z
@x Special classes for SAIL character set goodies
@d max_class=20 {the largest class number}
@y we add another class for `form feed'
@d max_class=21 {the largest class number}
@z
@x
for k←0 to " "-1 do char_class[k]←invalid_class;
@y
for k←0 to " "-1 do char_class[k]←invalid_class;
char_class[@'30]←10; {left arrow will join the class of \.{:=}}
char_class[@'32]←10; {not equal sign, likewise}
char_class[@'34]←10; {less than or equal sign as well}
char_class[@'35]←10; {greater than or equal sign too}
char_class[form_feed]←max_class; {form feed is in a class by itself}
@z
@x Screen routines:
begin init_screen←false;
@y
begin init_screen←true; {screen instructions will be logged}
@z
@x Page number maintenance:
If more information about the input state is needed, it can be
included in small arrays like those shown here. For example,
the current page or segment number in the input file might be
put into a variable |@!page|, maintained for enclosing levels in
`\ignorespaces|@!page_stack:array[1..max_in_open] of integer|\unskip'
by analogy with |line_stack|.
@y
Similarly, we maintain a global variable |page| and a corresponding
|page_stack|.
@z
@x
@!line_stack : array[1..max_in_open] of integer;
@y
@!line_stack : array[1..max_in_open] of integer;
@!page : integer; {current page number in the current source file}
@!page_stack : array[1..max_in_open] of integer;
@z
@x Printing the page number:
else	begin print_nl("l."); print_int(line);
@y
else	begin if page>1 then
		begin print_nl("p."); print_int(page); print(",l.");
		end
	else print_nl("l.");
	print_int(line);
@z
@x More page number maintenance:
or |limit| or |line|.
@y
or |limit| or |line| or |page|.
@z
@x
line_stack[index]←line; start←first;
@y
line_stack[index]←line; start←first; page_stack[index]←page;
@z
@x
begin first←start; line←line_stack[index];
@y
begin first←start; page←page_stack[index]; line←line_stack[index];
@z
@x Pausing on input:
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected, each line of input is displayed
on the terminal and the transcript file, followed by `\.{=>}'.
\MF\ waits for a response. If the response is null (i.e., if nothing is
typed except perhaps a few blank spaces), the original
line is accepted as it stands; otherwise the line typed is
used instead of the line in the file.

@p procedure firm_up_the_line;
var @!k:0..buf_size; {an index into |buffer|}
begin limit←last;
if internal[pausing]>0 then if interaction>nonstop_mode then
	begin wake_up_terminal; print_ln;
	if start<limit then for k←start to limit-1 do print(buffer[k]);
	first←limit; prompt_input("=>"); {wait for user response}
@.=>@>
	if last>first then
		begin for k←first to last-1 do {move line down in buffer}
			buffer[k+start-first]←buffer[k];
		limit←start+last-first;
		end;
	end;
end;
@y
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected,
each line of input is displayed in the transcript file, followed by `\.{=>}',
and also put into the user's line-editor buffer.
\MF\ waits for the line to be edited, and the next line received is
used instead of the line in the file.

@p procedure firm_up_the_line;
var @!k:0..buf_size; {an index into |buffer|}
begin limit←last;
if internal[pausing]>0 then if interaction>nonstop_mode then
 if buffer[start]≠form_feed then
	begin cancel_esc_O; {cancel the user's cancellation of output}
	print_ln;
	if start=limit then {empty line will be made nonempty so that it's visible}
		begin buffer[start]←" "; incr(limit);
		end;
	decr(selector); {inhibit terminal output temporarily}
	for k←start to limit-1 do
		begin print_char(buffer[k]);
		pto_chr(xchr[buffer[k]]);
		end;
	print("=>"); first←start;
	if not input_ln(term_in,true) then
		fatal_error("End of file on the terminal!");
@.End of file on the terminal@>
	if last>first then for k←first to last-1 do print_char(buffer[k]);
	limit←last; print_ln; incr(selector);
	end;
end;
@z
@x Parsing file names:
@ The file names we shall deal with for illustrative purposes have the
following structure:  If the name contains `\.>' or `\.:', the file area
consists of all characters up to and including the final such character;
otherwise the file area is null.  If the remaining file name contains
`\..', the file extension consists of all such characters from the first
remaining `\..' to the end, otherwise the file extension is null.
@↑system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.>' or `\.:', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@y
@ The file names we shall deal with have the following structure:
If the name contains `\.[', the file area consists of all characters
from this character to the end; otherwise the file area is null.
If the remaining file name contains `\..', the file extension consists of all
such characters from this character to the end, otherwise the file extension
is null. We can assume that there is at most one `\.[' and at most one `\..'.
@↑system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.[', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@z
@x
@d MF_area=="MFinputs:"
@.MFinputs@>
@y
@d MF_area=="[mf,sys]"
@z
@x
else	begin if (c=">")∨(c=":") then
		begin area_delimiter←pool_ptr; ext_delimiter←0;
		end
	else if (c=".")∧(ext_delimiter=0) then ext_delimiter←pool_ptr;
@y
else	begin if c="[" then area_delimiter←pool_ptr
	else if c="." then ext_delimiter←pool_ptr;
@z
@x
if area_delimiter=0 then cur_area←""
else	begin cur_area←str_ptr; incr(str_ptr);
	str_start[str_ptr]←area_delimiter+1;
	end;
if ext_delimiter=0 then
	begin cur_ext←""; cur_name←make_string;
	end
else	begin cur_name←str_ptr; incr(str_ptr);
	str_start[str_ptr]←ext_delimiter; cur_ext←make_string;
	end;
@y
cur_name←str_ptr;
if ext_delimiter=0 then cur_ext←""
else	begin incr(str_ptr);
	str_start[str_ptr]←ext_delimiter; cur_ext←str_ptr;
	end;
if area_delimiter≤str_start[str_ptr] then
	begin cur_area←""; incr(str_ptr); str_start[str_ptr]←pool_ptr;
	end
else	begin incr(str_ptr);
	str_start[str_ptr]←area_delimiter; cur_area←make_string;
	end;
@z
@x Printing file names:
begin print(a); print(n); print(e);
@y
begin print(n); print(e); print(a);
@z
@x Converting file names to PASCAL strings:
for j←str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
for j←str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j←str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
@y
j←".tfm"; {this keeps \.{mf.pool} the same as in production \MF}
for j←str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j←str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
for j←str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
@z
@x Parsing file names in the buffer:
@d base_default_length=18 {length of the |MF_base_default| string}
@d base_area_length=8 {length of its area part}
@d base_ext_length=5 {length of its `\.{.base}' part}
@y
@d base_default_length=17 {length of the |MF_base_default| string}
@d base_area_length=8 {length of its area part}
@d base_ext_length=4 {length of its `\.{.base}' part}
@z
@x
MF_base_default←'MFbases:PLAIN.base';
@.MFbases@>
@y
MF_base_default←'PLAIN.bas[mf,sys]';
@z
@x
|MF_base_default|.
@y
|MF_base_default|; but it actually switches stuff around to keep the
file area last.
@z
@x
@!j:integer; {index into |buffer| or |MF_base_default|}
@y
@!j:integer; {index into |buffer| or |MF_base_default|}
@!d:integer; {a kludge}
@z
@x
for j←1 to n do append_to_name(xord[MF_base_default[j]]);
for j←a to b do append_to_name(buffer[j]);
for j←base_default_length-base_ext_length+1 to base_default_length do
	append_to_name(xord[MF_base_default[j]]);
@y
for j←a to b do append_to_name(buffer[j]);
if b=0 then
	begin d←base_default_length-base_area_length+1;
	n←base_default_length;
	end
else d←base_default_length-base_area_length-base_ext_length+1;
for j←d to base_default_length-base_area_length do
	append_to_name(xord[MF_base_default[j]]);
for j←base_default_length-n+1 to base_default_length do
	append_to_name(xord[MF_base_default[j]]);
@z
@x The real file names:
@ Operating systems often make it possible to determine the exact name (and
@y we don't change them in BUGMF.
@ Operating systems often make it possible to determine the exact name (and
@z
@x Line editor gets misspelled file name:
begin if interaction=scroll_mode then wake_up_terminal;
@y
@!i:pool_pointer; {index into |str_pool|}
begin if interaction=scroll_mode then wake_up_terminal;
@z
@x
clear_terminal; prompt_input(": "); @<Scan file name in the buffer@>;
@y
clear_terminal; {now we'll fill the line editor's buffer with the old name}
for i←str_start[cur_name] to str_start[cur_name+1]-1 do
	pto_chr(xchr[str_pool[i]]);
for i←str_start[cur_ext] to str_start[cur_ext+1]-1 do
	pto_chr(xchr[str_pool[i]]);
for i←str_start[cur_area] to str_start[cur_area+1]-1 do
	pto_chr(xchr[str_pool[i]]);
ptwr1w(0,@'214); {control-formfeed returns cursor to start of line}
prompt_input(": "); @<Scan file name in the buffer@>;
@z
@x Reading the first line of a file:
@ Here we have to remember to tell the |input_ln| routine not to
start with a |get|. If the file is empty, it is considered to
contain a single blank line.
@↑system dependencies@>

@<Read the first line...@>=
begin if ¬ input_ln(cur_file,false) then do_nothing;
@y
@ Here we have to remember to tell |input_ln| not to do the first |get|,
and we also want to skip over the material on a file directory page. An
empty file is considered to contain a single blank line.
@↑system dependencies@>

@<Read the first line...@>=
begin if input_ln(cur_file,false) then
	begin if(last-start=29)∧(buffer[start]="C")∧(buffer[start+8]=@'26) then
		begin while (cur_file↑≠chr(form_feed))∧(not eof(cur_file)) do
			begin read_ln(cur_file); read(cur_file,aux_buf:temp_ptr);
			end; {skip the directory}
		buffer[start]←form_feed; last←start+1;
		end;
	end;
page←1;
@z
@x Opening the TFM file:
while ¬ b_open_out(tfm_file) do
@y
while ¬ tfm_b_open_out(tfm_file) do
@z
@x The GF output buffer:
@!gf_index=0..gf_buf_size; {an index into the output buffer}
@y
@!gf_index=0..gf_buf_size; {an index into the output buffer}
@!packed_bytes=packed array[gf_index] of eight_bits;
	{buffer for \.{GF} output}
@z
@x
@!gf_buf:array[gf_index] of eight_bits; {buffer for \.{GF} output}
@y
@!gf_buf:packed_bytes; {buffer for \.{GF} output}
@z
@x
procedure write_gf(@!a,@!b:gf_index);
var k:gf_index;
begin for k←a to b do write(gf_file,gf_buf[k]);
end;
@y
procedure ary_out(var @!f:file;@!b:packed_bytes; @!o,@!c:integer);
	extern;@t\2@>@#
procedure write_gf(@!a,@!b:gf_index);
begin ary_out(gf_file,gf_buf,a div 4,(b+1-a)div 4);@{+1000@}
end;
@z
@x "r GFtoDVI":
b_close(gf_file);
@y
b_close(gf_file);
if pseudo_typein=0 then if internal[proofing]>0 then
	begin k←selector; selector←new_string;
	pool_ptr←str_start[str_ptr];
	print("r GFtoDVI;"); print(output_file_name);
	selector←k;
	if pool_ptr<pool_size then
	 if str_ptr<max_strings then {|overflow| can't occur}
		pseudo_typein←make_string;
	end;
@z
@x start/stop counting
@p begin @!{|start_here|}
@y
@p begin @!{|start_here|}
@!freq magic_begin;@+qerf
@z

@x
final_end: ready_already←0;
@y
final_end: ready_already←0;
@!freq magic_end;@+qerf
@z
@x The endgame:
@<Last-minute...@>=
@y
The new stuff at {\mc SAIL} has to do with preparing for what the user
presumably wants to do next, by typing it for him/her.

@<Last-minute...@>=
@z
@x
end;
@y
if (pseudo_typein≠0)∧(interaction>batch_mode) then
	begin write_ln(term_out);
	for k←str_start[pseudo_typein] to str_start[pseudo_typein+1]-1 do
		pto_chr(xchr[str_pool[k]]);
	end;
end;
@z
@x Final system-dependent changes:
This section should be replaced, if necessary, by changes to the program
that are necessary to make \MF\ work at a particular installation.
It is usually best to design your change file so that all changes to
previous sections preserve the section numbering; then everybody's version
will be consistent with the published program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@↑system dependencies@>
@y
Here are the remaining things needed to make the implementation
complete at {\mc SAIL}.
@↑system dependencies@>

@ The |pseudo_typein| variable is set nonzero if the |error| routine
uses the `\.E' option to exit and edit.

@<Glob...@>=
@!pseudo_typein:str_number;

@ @<Set init...@>=
pseudo_typein←0; page←0;

@ The next sections of the program contain frequency counting system,
due to David Fuchs.
@↑Fuchs, David Raymond@>

Frequency counts go to a special file.

@<Glob...@>=
@!freq @!count_file:alpha_file; {the number of counts, followed by their values}
qerf

@ Here's a type that facilitates system hackery. (\MF\ doesn't actually
use all of these variants.)

@d dec_word==packed record
	case integer of
	0: (@!xx:0..@'777777; @!p: ↑integer);
	1: (@!z: integer);
	2: (@!lh:0..@'777777; @!rh:0..@'777777);
	3: (@!sixbit: packed array[1..6] of 0..@'77)
	end

@<Glob...@>=
@!hack,@!memry: dec_word; {two more temporaries}
@!num_counts: integer; {number of counters}
@!job_hrl: integer; {highest address of this job}

@ We can look through memory for counters, with some trickery.

@<Get ready to look for count locations@>=
hack.z←@'115; hack.z←hack.p↑; job_hrl←hack.rh;
memry.z←@'400000; {set up memory search}

@ Here's how to locate the next counter.  We leave a pointer to its
location in |memry|, and return |false| if there are no more.

@d AOSA_code==@'354000 {machine-language op code for \.{AOSA}}

@<Last-minute procedures@>=
@!freq function next_count: boolean;
label done;
begin
next_count←true;
while memry.z<job_hrl do
	begin
	incr(memry.z);
	hack.z←memry.p↑;
	if (hack.lh=AOSA_code) and (hack.rh=memry.z+1) then
		begin incr(memry.z); {skip to the counter}
		goto done;
		end;
	end;
next_count←false;
done:
end;
qerf

@ The procedure |magic_begin| reads in the old counts, if any.

@<Last-minute procedures@>=
@!freq procedure magic_begin;
var @!success: boolean; {temporary}
@!i:integer;
begin
@<Get ready to look for count locations@>;
@<Read in the old counts, if possible@>;
end;
qerf

@ We have to simultaniously read in counts and find where in memory
they go.

@<Read in the old counts, if possible@>=
reset(count_file,count_name,'/O'); {set up count file}
if not eof(count_file) then
	begin
	read_ln(count_file,num_counts);
	write_ln(term_out,'Reading in ',num_counts:1,' counts');
	i←0;
	while not eof(count_file) do begin
		success←next_count;
		read_ln(count_file,memry.p↑);
		incr(i);
		end;
	close(count_file);
	if success then success←not next_count; {shouldn't be any more counts}
	if num_counts≠i then success←false;
	if not success then begin
		write_ln(term_out,'Bad counter file length');
		goto final_end;
		end;
	end
else begin
	num_counts←0;
	while next_count do incr(num_counts);
	write_ln(term_out,'Initializing ',num_counts:1,' zero counts');
	end;

@ The procedure |magic_end| writes out the new counts.

@<Last-minute procedures@>=
@!freq procedure magic_end;
begin
@<Get ready to look for count locations@>;
@<Write out a new count file@>;
end;
qerf

@ @<Write out a new count file@>=
@<Get ready to look for count locations@>;
rewrite(count_file,count_name); {now we'll write out the counts}
write_ln(term_out);
write_ln(term_out,'Writing ',num_counts:1,' count file');
write_ln(count_file,num_counts);
while next_count do write_ln(count_file,memry.p↑:1);
close(count_file);

@ OK, that was the end of the frequency counting stuff. Finally,
a global variable is needed for our ESC/BREAK hack.

@<Glob...@>=
@!esc_break: array [1..1] of integer;

@ And still more finally, here's a ``frozen'' definition that makes end-of-page
act like an ``outer'' semicolon.

@<Put each...@>=
text(form_feed+1)←form_feed;
eq_type(form_feed+1)←semicolon+outer_tag;
@z